﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Net.NetworkInformation;
using System.Net;
using System.Management;
using System.Net.Sockets;

namespace RNT.Common.Utils
{
    /// <summary>
    /// 计算机信息获取类
    /// </summary>
    public class ComputerInfos
    {
        /// <summary>
        /// 获取计算机名称
        /// </summary>
        public static string GetHostName()
        {
            return Dns.GetHostName();
        }

        /// <summary>
        /// 获取处理器(CPU)序列号
        /// </summary>
        public static string GetProcessID()
        {
            string processId = String.Empty;
            ManagementClass procmc = new ManagementClass("Win32_Processor");
            ManagementObjectCollection procmoc = procmc.GetInstances();
            foreach (ManagementObject mo in procmoc)
            {
                processId = mo["ProcessorId"].ToString();
                break;
            }
            procmoc.Dispose();
            return processId;
        }


        /// <summary>
        /// 获取物理网卡地址
        /// </summary>
        /// <returns>默认返回获取首个匹配值</returns>
        public static string GetMacAddress()
        {
            string macAddress = String.Empty;
            ManagementClass netmc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection netmoc = netmc.GetInstances();
            foreach (ManagementObject mo in netmoc)
            {
                if ((bool)mo["IPEnabled"])
                {
                    macAddress = mo["MacAddress"].ToString();
                    break;
                }
            }
            netmoc.Dispose();
            return macAddress;
        }

        /// <summary>
        ///  获取运行系统名称
        /// </summary>
        /// <returns>系统名称或 "unknown"</returns>
        public static string GetOSName()
        {
            /*
                操作系统	PlatformID	主版本号	副版本号
                Windows95	1	4	0
                Windows98	1	4	10
                WindowsMe	1	4	90
                WindowsNT3.5	2	3	0
                WindowsNT4.0	2	4	0
                Windows2000	2	5	0
                WindowsXP	2	5	1
                Windows2003	2	5	2
                WindowsVista	2	6	0
                Windows7	2	6	1
                Windows8	2 	6 	2 
             */

            string osname = "unknown";
            OperatingSystem osInfo = Environment.OSVersion;



            // 判断平台
            switch (osInfo.Platform)
            {
                // Windows 95, Windows 98,Windows 98 Second Edition, or Windows Me
                case System.PlatformID.Win32Windows:

                    switch (osInfo.Version.Minor)
                    {
                        case 0:
                            osname = "Windows 95";
                            break;
                        case 10:
                            if (osInfo.Version.Revision.ToString() == "2222A")
                            {
                                osname = "Windows 98 Second Edition";
                            }
                            else
                            {
                                osname = "Windows 98";
                            }
                            break;
                        case 90:
                            osname = "Windows Me";
                            break;
                    }
                    break;

                // Windows NT 3.51, Windows NT 4.0, Windows 2000, Windows XP. Windows Vista, Windows 7
                case System.PlatformID.Win32NT:

                    switch (osInfo.Version.Major)
                    {
                        case 3:
                            osname = "Windows NT 3.51";
                            break;
                        case 4:
                            osname = "Windows NT 4.0";
                            break;
                        case 5:
                            if (osInfo.Version.Minor == 0)
                            {
                                osname = "Windows 2000";
                            }
                            else
                            {
                                osname = "Windows XP";
                            }
                            break;
                        case 6:
                            if (osInfo.Version.Minor == 0)
                            {
                                osname = "Windows Vista";
                            }
                            else if(osInfo.Version.Minor == 1)
                            {
                                osname = "Windows 7";
                            }
                            else if (osInfo.Version.Minor == 2)
                            {
                                osname = "Windows 8";
                            }
                            break;
                    }
                    break;
            }
            return osname;
        }


        /// <summary>
        /// 获取 IPAddress 地址
        /// </summary>
        public static IList<IPAddress> GetIPAddress()
        {
            IList<IPAddress> lstAddress = new List<IPAddress>();
            NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface ni in nis)
            {
                IPInterfaceProperties ipp = ni.GetIPProperties();
                UnicastIPAddressInformationCollection uipic = ipp.UnicastAddresses;
                foreach (UnicastIPAddressInformation uipi in uipic)
                {
                    if (uipi.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        lstAddress.Add(uipi.Address);
                    }
                }
            }
            return lstAddress;
        }
    }
}
